package com.yunxun.amqp;
import com.rabbitmq.client.*;
import com.yunxun.utils.ChannelUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.HashMap;


//@Component
public class RPCMessageConsumer {

//    @RabbitHandler
//    @RabbitListener(queues = "messageQueue")
    public void process(String hello) {
        System.out.println("Receiver1 :" + hello);
//        Consumer consumer = new DefaultConsumer(channel) {
//            @Override
//            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                AMQP.BasicProperties replyProps = new AMQP.BasicProperties
//                        .Builder()
//                        .correlationId(properties.getCorrelationId())
//                        .build();
//
//                String response = "1234";
//                try {
//                    String message = new String(body, "UTF-8");
//                    int n = Integer.parseInt(message);
//                    System.err.println("--------------"+message);
//                } catch (RuntimeException e) {
//                    System.out.println(" [.] " + e.toString());
//                } finally {
//                    channel.basicPublish("", properties.getReplyTo(), replyProps, response.getBytes("UTF-8"));
//                    channel.basicAck(envelope.getDeliveryTag(), false);
//                    // RabbitMq consumer worker thread notifies the RPC server owner thread
//                    synchronized (this) {
//                        this.notify();
//                    }
//                }
//            }
//        };
//        try {
//            channel.basicConsume("messageQueue",true,"hellow", consumer);
//            while (true) {
//                synchronized (consumer) {
//                    try {
//                        consumer.wait();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//    } catch(IOException e) {
//        e.printStackTrace();
//    }
//    finally {
//        if (connection != null) {
//            connection.close();
//           }
//        }
    }

}
